package com.quanroon.ysq.master_worker.test;

import cn.hutool.system.SystemUtil;
import com.quanroon.ysq.master_worker.Master;
import com.quanroon.ysq.master_worker.Worker;

import java.util.Map;
import java.util.Set;

/**
 * @author quanroong.ysq
 * @version 1.0.0
 * @description
 * @createtime 2021/10/20 22:12
 */
public class PlusWorker extends Worker {
    //实现具体的逻辑， 即求立方和
    @Override
    public Object handle(Object input) {
        Integer i = (Integer)input;
        return i*i*i;
    }

    public static void main(String[] args) {
        //固定5个worker
        Master master = new Master(new PlusWorker(), 5);

        //提交100个子任务
        for (int i = 0; i < 100; i++){
            master.submit(i);
        }
        master.execute();

        int re = 0;

        //对结果进行处理
        Map<String, Object> resultMap = master.getResultMap();

        while (resultMap.size() > 0 || !master.isComplete()){
            Set<String> keys = resultMap.keySet();
            String key = null;
            for (String k : keys) {
                key = k;
                break;
            }

            Integer i = null;
            if(key != null){
                i = (Integer) resultMap.get(key);
            }
            if(i != null){
                re += i;
            }
            if(key != null){
                resultMap.remove(key);
            }
        }
        System.out.println("最终结果：" +re);
    }
}
